package com.xiantm.study.thread.singlethreadexecution;

public class Gate{
    private String name = "Nobody";
    private int count = 0;
    private String address = "Nowhere";

    public synchronized void pass(String name, String address) {
        this.count ++;
        this.name = name;
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        this.address = address;
        check();
    }

    @Override
    public synchronized String toString() {
        return "No." + count + ": "+ name + ", " + address;
    }


//    public  void pass(String name, String address) {
//        this.count ++;
//        this.name = name;
//        try {
//            Thread.sleep(1000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        this.address = address;
//        check();
//    }
//
//    @Override
//    public  String toString() {
//        return "No." + count + ": "+ name + ", " + address;
//    }

    private void check(){
        if (name.charAt(0)!= address.charAt(0)){
            System.out.println("********** Broken ********* " + toString());
        }
    }
}